#include "stdafx.h"
#include "DropPropertyPage.h"
#include "Property.h"
#include "EventManagerSngl.h"
#include "EventCodes.h"
#include "DeclareList.h"
#include "Town.h"

#include <utility/Config.h>
#include <utility/Streams.h>

#ifdef WIN32
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif

namespace serika
{
	namespace dq
	{
		DropPropertyPage::DropPropertyPage(PropertyTemplate* tmpl)
			: PropertyPage(tmpl)
		{
		}

		DropPropertyPage::~DropPropertyPage()
		{
		}

		void DropPropertyPage::OnInitialize()
		{
			this->CreateCombo(mCombo, CBS_DROPDOWNLIST);
			EventManager::AttachLogicEventHandler(this);
			EventManager::SendEvent(ut::EventType::UI, UEC::RequestTownList);
			EventManager::DetachLogicEventHandler(this);
		}

		void DropPropertyPage::DoLayout(ut::Config* config)
		{
			this->Layout(&mCombo, config, _T("Combo"));
		}

		Property* DropPropertyPage::CreateProperty()
		{
			int id = mCombo.GetCurSel() + 1;
			CString text;
			mCombo.GetWindowText(text);
			return Property::NewProperty(0, id, 0, 0, ut::String(text), mTmpl);
		}

		void DropPropertyPage::OnActivate()
		{
		}

		void DropPropertyPage::OnDeactivate()
		{
		}

		void DropPropertyPage::Handle(const ut::Event& e)
		{
			if (LEC::TownList != e.GetCode())
			{
				return;
			}

			const Towns& towns = *e.GetUserData<Towns>();
			for (Towns::const_iterator itr = towns.begin();
				itr != towns.end(); ++itr)
			{
				Town* town = *itr;
				mCombo.InsertString(town->GetId()-1, town->GetChinessName().c_str());
			}
		}
	}
}
